package com.leetao.dingtalk.core.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.leetao.dingtalk.core.config.DingTalkProperties;
import com.leetao.dingtalk.core.domain.MessageCorpconversation;
import com.leetao.dingtalk.core.domain.rest.Rest;
import com.leetao.dingtalk.core.manager.DingTalkTokenManager;
import com.leetao.dingtalk.core.service.DingTalkNotificationService;
import com.leetao.dingtalk.core.support.DingTalkResponseToRestConverter;
import com.taobao.api.ApiException;

import java.util.Arrays;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.leetao.dingtalk.core.constant.DingTalkNotificationConstants.*;
import static com.leetao.dingtalk.core.constant.DingTalkApis.*;
import static com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.*;


/**
 * 钉钉消息通知
 * @author 菅江晖
 * @date 2021/5/24 - 14:30
 */
public class DingTalkNotificationServiceImpl implements DingTalkNotificationService {

    final private DingTalkProperties dingTalkProperties;
    final private DingTalkTokenManager talkTokenManager;


    public DingTalkNotificationServiceImpl(DingTalkProperties dingTalkProperties, DingTalkTokenManager talkTokenManager) {
        this.dingTalkProperties = dingTalkProperties;
        this.talkTokenManager = talkTokenManager;
    }

    @Override
    public Rest<MessageCorpconversation> sendJobNotificationTextMessagesToEveryone(String content) throws ApiException {
        return sendJobNotificationTextToWhere(content,req -> req.setToAllUser(true));

    }

    @Override
    public Rest<MessageCorpconversation> sendJobNotificationTextMessagesToDeptIds(String content, String... deptIds) throws ApiException {
        return sendJobNotificationTextToWhere(content,req -> {
            req.setDeptIdList(String.join(MULTIPLE_PARAMETER_SEPARATOR,deptIds));
        });

    }

    /**
     * 发送文本工作通知给？
     * @param content 发送内容
     * @param consumer 消费者
     * @return Rest<MessageCorpconversation>
     * @throws ApiException ApiException
     */
    private Rest<MessageCorpconversation> sendJobNotificationTextToWhere(String content, Consumer<OapiMessageCorpconversationAsyncsendV2Request> consumer) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(SEND_WORK_NOTIFICATIONS_ASYNCHRONOUSLY);
        OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
        consumer.accept(req);
        req.setAgentId(dingTalkProperties.getAgentId());

        Msg msg = new Msg();
        msg.setMsgtype(TEXT);
        Text text = new Text();
        text.setContent(content);
        msg.setText(text);
        req.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, talkTokenManager.getTokenRest().getData().getAccessToken());

        return DingTalkResponseToRestConverter.convertMessageCorpconversationRest(rsp);
    }

}
